﻿#include "printerManager.h"


// Provider =========================================

PrintPreviewProvider::PrintPreviewProvider() : QQuickImageProvider(QQuickImageProvider::Image)
{
}

QImage PrintPreviewProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize){
    return *pm;
}


// PrinterManager ===================================


PrinterManager::PrinterManager()
{
    image = new QImage(BW, BH, QImage::Format_RGB888);
    image->fill(QColor(Qt::white));

    font1 = QFont("宋体", 9);
    font1.setBold(true);
    font2 = QFont("宋体", 9);
    font2.setBold(false);

}


PrinterManager& PrinterManager::I(){
    static PrinterManager instance;
    return instance;
}

void PrinterManager::initProvider(QQmlApplicationEngine *engine){
    provider = new PrintPreviewProvider();
    provider->pm = image;
    engine->addImageProvider(QLatin1String("printPreviewImage"), provider);
}

QStringList PrinterManager::getPrinterList(){
    auto res = QPrinterInfo::availablePrinterNames();
    return res;
}



void PrinterManager::print(QString printerName){
    qDebug() << "打印信息" << printerName;
    QPrinter printer(QPrinter::ScreenResolution);
    printer.setPrinterName(printerName);

    QString htmlText = generateHTML();
    QTextDocument td;
    td.setHtml(htmlText);
    td.print(&printer);

    qDebug() << "打印完成";



}

void PrinterManager::refreshPreview(double dp){

    this->dp = dp;

    if(image) {delete image;}

    image = new QImage(std::floor(BW*dp), std::floor(BH*dp), QImage::Format_RGB888);
    image->fill(QColor(Qt::white));
    provider->pm = image;

    expInfo = ReportManager::I().getExperimetnInfo();

    QPainter* painter = generatePainter();

    qDebug() << "dp" << dp << "size" << image->size();

    delete painter;
}

QPainter* PrinterManager::generatePainter(){
    QPainter* painter = new QPainter(image);


    painterDrawExpInfo(painter);
    painterDrawExpLog(painter);

    return painter;
}


void PrinterManager::painterDrawExpLog(QPainter *painter){
    int mW = 80*dp;
    int mH = 80*dp;
    int W = BW*dp - 2*mW;
    int lineH = 35*dp;
    int H = lineH*4.5;


    painter->setPen(QColor(0, 0, 0));
    painter->drawText(QPoint(mW, mH-10*dp), "中国科学院大连化学物理研究所");
    QImage image_code(":/picture/tm.png");
    painter->drawImage(QRect(mW+W-100*dp, mH-40*dp, 100*dp, 40*dp), image_code);
    painter->setFont(font1);
    painter->drawText(QPoint(mW, mH+30*dp), "综合报告");
    painter->setFont(font2);
    painter->drawText(QPoint(mW+W-240, mH+30*dp), "打印时间: " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));

    painter->drawRect(mW, mH+40*dp, W, H);      // 外框矩形
    painter->drawText(QPoint(mW+8*dp, mH+62*dp+lineH*0), "耗材名称");
    painter->drawText(QPoint(mW+8*dp, mH+62*dp+lineH*1), "名称");
    painter->drawText(QPoint(mW+8*dp, mH+62*dp+lineH*2), "实验日期");
    painter->drawText(QPoint(mW+8*dp, mH+62*dp+lineH*3), "耗材编码");
//    painter->drawText(QPoint(mW+8*dp, mH+62*dp+lineH*4), "实验名称");
    painter->drawText(QPoint(mW+8*dp+W/2, mH+62*dp+lineH*0), "实验名称");
    painter->drawText(QPoint(mW+8*dp+W/2, mH+62*dp+lineH*1), "设备名称");
    painter->drawText(QPoint(mW+8*dp+W/2, mH+62*dp+lineH*2), "操作人");
    painter->drawText(QPoint(mW+8*dp+W/2, mH+62*dp+lineH*3), "描述");
//    painter->drawText(QPoint(mW+8*dp+W/2, mH+62*dp+lineH*4), "实验方法");

    painter->drawText(QPoint(mW+4*dp+W/4, mH+62*dp+lineH*0),   expInfo["ConsumableName"]);
    painter->drawText(QPoint(mW+4*dp+W/4, mH+62*dp+lineH*1),   expInfo["ExperimentName"]);
    painter->drawText(QPoint(mW+4*dp+W/4, mH+62*dp+lineH*2),   expInfo["ExecDate"]);
    painter->drawText(QPoint(mW+4*dp+W/4, mH+62*dp+lineH*3),   expInfo["ConsumableCode"]);
//    painter->drawText(QPoint(mW+4*dp+W/4, mH+62*dp+lineH*4),   expInfo["ConsumableCode"]);
    painter->drawText(QPoint(mW+4*dp+W/4*3, mH+62*dp+lineH*0), expInfo["ExperimentName"]);
    painter->drawText(QPoint(mW+4*dp+W/4*3, mH+62*dp+lineH*1), expInfo["EquipmentName"]);
    painter->drawText(QPoint(mW+4*dp+W/4*3, mH+62*dp+lineH*2), expInfo["Operator"]);
    painter->drawText(QPoint(mW+4*dp+W/4*3, mH+62*dp+lineH*3), expInfo["Memo"]);
//    painter->drawText(QPoint(mW+4*dp+W/4*3, mH+62*dp+lineH*4), expInfo["Method"]);

    // 底下的 那个线
    painter->drawLine(QPoint(mW+W/4, mH+64*dp+lineH*0), QPoint(mW-8*dp+W/2, mH+64*dp+lineH*0));
    painter->drawLine(QPoint(mW+W/4, mH+64*dp+lineH*1), QPoint(mW-8*dp+W/2, mH+64*dp+lineH*1));
    painter->drawLine(QPoint(mW+W/4, mH+64*dp+lineH*2), QPoint(mW-8*dp+W/2, mH+64*dp+lineH*2));
    painter->drawLine(QPoint(mW+W/4, mH+64*dp+lineH*3), QPoint(mW-8*dp+W/2, mH+64*dp+lineH*3));
//    painter->drawLine(QPoint(mW+W/4, mH+64*dp+lineH*4), QPoint(mW-8*dp+W/2, mH+64*dp+lineH*4));

    painter->drawLine(QPoint(mW+W/4*3, mH+64*dp+lineH*0), QPoint(mW-8*dp+W, mH+64*dp+lineH*0));
    painter->drawLine(QPoint(mW+W/4*3, mH+64*dp+lineH*1), QPoint(mW-8*dp+W, mH+64*dp+lineH*1));
    painter->drawLine(QPoint(mW+W/4*3, mH+64*dp+lineH*2), QPoint(mW-8*dp+W, mH+64*dp+lineH*2));
    painter->drawLine(QPoint(mW+W/4*3, mH+64*dp+lineH*3), QPoint(mW-8*dp+W, mH+64*dp+lineH*3));
//    painter->drawLine(QPoint(mW+W/4*3, mH+64*dp+lineH*4), QPoint(mW-8*dp+W, mH+64*dp+lineH*4));


}

void PrinterManager::painterDrawExpInfo(QPainter *painter){

    int mW = 80*dp;
    int mH = 80*dp;
    int W = BW*dp - 2*mW;
    int lineH = 32*dp;

    painter->setFont(font1);
    painter->drawText(QPoint(mW, 330*dp), "实验步骤");
    painter->setFont(font2);
    painter->drawRect(mW, 340*dp, W, BH*dp-mH-340*dp);
    painter->drawText(QPoint(mW+8*dp+W*0.09, 365*dp+lineH*0), "时间");
    painter->drawText(QPoint(mW+8*dp+W*0.24, 365*dp+lineH*0), "步骤编号");
    painter->drawText(QPoint(mW+8*dp+W*0.60, 365*dp+lineH*0), "动作信息");
    painter->drawText(QPoint(mW+8*dp+W*0.89, 365*dp+lineH*0), "状态");

    int i = 1;
    foreach(int key, expLogInfo.keys()){
        int y = 380*dp + lineH*i;
        if(y < BH*dp-mH - 10*dp){

            QVariantMap map = expLogInfo[key];
            if(!map.contains("FinishTime") || !map.contains("FinishTime") || !map.contains("FinishTime") || !map.contains("FinishTime")){
                qDebug() << "ERROR: [Class]ReportManager [Function]QueryReportInfo - expLogInfo loss key";
                continue;
            }
            QString info1 = map["FinishTime"].value<QString>();
            QString info2 = map["Step"].value<QString>();
            QString info3 = map["ActionInfo"].value<QString>();
            QString info4 = map["Status"].value<QString>();
            if(info1 == ""){
                info1 = "--:--:--";
            }
            painter->drawText(QPoint(mW+8*dp+W*0.07, 365*dp+lineH*i), info1);
            painter->drawText(QPoint(mW+8*dp+W*0.28, 365*dp+lineH*i), info2);
            painter->drawText(QPoint(mW+8*dp+W*0.4,  365*dp+lineH*i), info3);
            painter->drawText(QPoint(mW+8*dp+W*0.87, 365*dp+lineH*i), info4);
        }else{
            break;
        }
        i++;
    }

}


QString PrinterManager::generateHTML(){

    QString htmlText;

    // expInfo信息校验
//    for(int i = 0; i < 10; i ++){
//        if(expInfo.size() < i){
//            expInfo.push_back("");
//        }
//    }

    htmlText += QString("<div style='height: 30;'>中国科学院大学大连化学物理研究所</div>"
                        "<div style='width: %12;'>"
                        "<p><span style='font-weight: bold;'>综合报告</span><span style='float: right;'>（打印时间:%11）</span></p>"
                        "<table width=100% border=1 cellpadding=5 cellspacing=0 style='font-weight: normal;'><thead></thead><tbody><tr><th style='font-weight: normal; border: 0;'>名称</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%1</th><th style='font-weight: normal; border: 0;'>设备型号</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%2</th></tr><tr><th style='font-weight: normal; border: 0;'>操作人员</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%3</th><th style='font-weight: normal; border: 0;'>日期</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%4</th></tr><tr><th style='font-weight: normal; border: 0;'>开始时间</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%5</th><th style='font-weight: normal; border: 0;'>结束时间</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%6</th></tr><tr><th style='font-weight: normal; border: 0;'>实验时长</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%7</th><th style='font-weight: normal; border: 0;'>描述</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%8</th></tr><tr><th style='font-weight: normal; border: 0;'>实验名称</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%9</th><th style='font-weight: normal; border: 0;'>实验方法</th><th style='font-weight: normal; border-top: 0; border-left: 0; border-right: 0;'>%10</th></tr></tbody></table>"
                        "<p style='font-weight: bold;'>实验步骤</p>"
                        "<table width=100% border=1 cellpadding=5 cellspacing=0 style=''>"
                        "<tbody><tr><th style='font-weight: normal; border: 0;'>时间</th><th style='font-weight: normal; border: 0;'>步骤编号</th><th style='font-weight: normal; border: 0;'>动作信息</th><th style='font-weight: normal; border: 0;'>状态</th></tr>")
            .arg(expInfo["Name"], expInfo["Equipment"], expInfo["Operator"], expInfo["Date"], expInfo["StartTime"],
                 expInfo["EndTime"], expInfo["ExecTime"], expInfo["Memo"], expInfo["ExpName"], expInfo["Method"],
                 QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"), QString::number(600));

    for(const auto &key : expLogInfo.keys()){
        QVariantMap map = expLogInfo[key];
        if(!map.contains("FinishTime") || !map.contains("FinishTime") || !map.contains("FinishTime") || !map.contains("FinishTime")){
            qDebug() << "ERROR: [Class]ReportManager [Function]QueryReportInfo - expLogInfo loss key";
            continue;
        }
        QString info1 = map["FinishTime"].value<QString>();
        QString info2 = map["Step"].value<QString>();
        QString info3 = map["ActionInfo"].value<QString>();
        QString info4 = map["Status"].value<QString>();
        htmlText += QString("<tr><th style='font-weight: normal; border: 0;'>%1</th><th style='font-weight: normal; border: 0;'>%2</th><th style='font-weight: normal; border: 0;'>%3</th><th style='font-weight: normal; border: 0;'>%4</th></tr>")
                .arg(info1, info2, info3, info4);
    }

    htmlText += "</tbody></table></div>";

    return htmlText;

}

